package qj.concurrent;

import java.util.List;
import java.util.ArrayList;

/**
 * Extends this class and declare run method to use in ServantManager.doServe()
 * Created by QuanLA
 * Date: Mar 15, 2006
 * Time: 9:48:35 AM
 */
public class ServantTask {
	private String taskName;
    private boolean cancelled;
    private List listeners = new ArrayList();
    
    private Object _task;

    public List getListeners() {
        return listeners;
    }

    public void addListener(TaskProgressListenable listener) {
        listeners.add(listener);
    }

    protected ServantTask(TaskProgressListenable listener) {
        listeners.add(listener);
    }

    protected ServantTask() {
    }
    
    public ServantTask(Task task, TaskProgressListenable listener) {
        listeners.add(listener);
        this._task = task;
    }

    public ServantTask(Task task) {
    	this._task = task;
    }

    public ServantTask(Runnable task) {
    	this._task = task;
	}

	public boolean isCancelled() {
        return cancelled;
    }

    public boolean cancel() {
        cancelled = true;
        return cancelled;
    }

    public void run() throws Exception {
    	if (_task instanceof Runnable)
    		((Runnable)_task).run();
    	else
    		((Task)_task).run();
//    	Method run = _task.getClass().getMethod("run", new Class[] {});
//    	run.invoke(_task, null);
    }

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

}
